<?php
/**
 * 商品控制器
 * @author maniac
 */
namespace app\shop\controller;

use app\common\model\GoodCategory;
use app\common\model\GoodMods;
use app\common\model\GoodBrands;
use app\common\model\GoodSuppliers;
use app\common\model\Stores;
use app\common\model\GoodSku AS GoodSkuModel;
use app\facade\GoodSku;
use app\facade\GoodAttrValue;
use app\facade\GoodMods AS GoodModsFacade;

class Goods extends Base
{
    public function alarm()
    {
        // 当前页数
        $page   = input('get.page/d', 1);
        // 模型
        $model  = $this->getModel();
        // 查询条件
        $where  = 'goods_stock<=goods_stock_alarm';
        // 排序条件
        $order  = $this->order();
        // 数据总数
        $count  = $model::where($where)->count();
        // 每页条数
        $limit  = $this->limit();
        // 分页条件
        $offset = $this->offset($page, $limit);
        // 分页数据
        $data   = $model::field(true)->where($where)->order($order)->limit($offset)->select();

        return $this->fetch('', [
            'page'  => $page,
            'count' => $count,
            'limit' => $limit,
            'data'  => $data,
        ]);
    }
    
    /**
     * 列表页附件
     * @param  array $data 列表页数据
     */
    public function indexAttach($data)
    {
        $categories = GoodCategory::field('id,parent_id,name,is_last')->order('id ASC')->select();
        $categories = recursion_one_array($categories);
        $this->assign('categories', $categories);
    }
    
    /**
     * 添加/编辑附加
     * @param array $info 要编辑的信息
     */
    public function addAttach($info)
    {
		$shop_id =	$this->shop_id();
        $categories = GoodCategory::field('id,parent_id,name,is_last')->order('id ASC')->select();
        $categories = recursion_one_array($categories);
        $goodMods   = GoodMods::field('id,name')->where('is_show', 1)->order('list_order DESC,id ASC')->select();
        $brands     = GoodBrands::field('id,name')->where('is_show', 1)->order('list_order DESC,id ASC')->select();
        $suppliers  = GoodSuppliers::field('id,name')->order('list_order DESC,id ASC')->select();
        $stores  = Stores::field('id,name')->where('id',$shop_id)->order('list_order DESC,id ASC')->select();
        $data       = [];
        if ($info && $info['good_mods_id']) {
            $data = GoodModsFacade::mod($info['good_mods_id']);
        }
        
        // 商品 sku
        $sku  = '';
        // 商品属性
        $attr = '';
        if ($info['id']) {
            $where = [
                'store_id' => $info['store_id'],
                'goods_id' => $info['id'],
            ];
            $sku  = GoodSku::sku($where);
            $attr = GoodAttrValue::attrValue($where);
        }
        
        $this->assign('categories', $categories);
        $this->assign('goodMods', $goodMods);
        $this->assign('brands', $brands);
        $this->assign('suppliers', $suppliers);
        $this->assign('data', $data);
        $this->assign('sku', $sku);
        $this->assign('attr', $attr);
        $this->assign('stores', $stores);
    }
    
    /**
     * 组合 where 查询条件
     */
    public function where()
    {
    	$shop_id =	$this->shop_id();
        $category_id  = input('get.category_id/d', 0);
        $is_sale      = input('get.is_sale/d', -1);
        $is_latest    = input('get.is_latest/d', -1);
        $is_hot_sale  = input('get.is_hot_sale/d', -1);
        $is_recommand = input('get.is_recommand/d', -1);
        $keywords     = input('get.keywords/s', '');
        $where[]        = [
        	['store_id','eq',$shop_id],
		];
        if ($category_id) {
            $where[] = [
                ['category_id', 'eq', $category_id],
            ];
        }
        
        if ($is_sale != -1) {
            $where[] = [
                ['is_sale', 'eq', $is_sale],
            ];
        }
        
        if ($is_latest != -1) {
            $where[] = [
                ['is_latest', 'eq', $is_latest],
            ];
        }
        
        if ($is_hot_sale != -1) {
            $where[] = [
                ['is_hot_sale', 'eq', $is_hot_sale],
            ];
        }
        
        if ($is_recommand != -1) {
            $where[] = [
                ['is_recommand', 'eq', $is_recommand],
            ];
        }
        
        if ($keywords) {
            $where[] = [
                ['goods_name', 'like', '%'.$keywords.'%'],
            ];
        }

        $this->assign('category_id', $category_id);
        $this->assign('is_sale', $is_sale);
        $this->assign('is_latest', $is_latest);
        $this->assign('is_hot_sale', $is_hot_sale);
        $this->assign('is_recommand', $is_recommand);
        $this->assign('keywords', $keywords);
        return $where;
    }
    
    /**
     * 每页条数
     */
    public function limit()
    {
        return 10;
    }
}
